package com.wd.buycar.view.fragment


import android.os.Build
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.view.PicSelectDialog
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.luck.picture.lib.thread.PictureThreadUtils
import com.wd.buycar.databinding.FragmentBuyNewCarBinding
import com.wd.buycar.view.adapter.BuynewcarHAdapterList
import com.wd.buycar.view.adapter.BuynewcarLiShiAdapterList
import com.wd.buycar.view.adapter.BuynewcarVAdapterList
import com.wd.common.base.BaseFragment
import com.wd.common.viewmodel.MyViewModel
import com.wd.find.FindViewModel


class BuyNewCarFragment : BaseFragment<FragmentBuyNewCarBinding>() {
    val viewmodel: MyViewModel by viewModels()
    private lateinit var photoDialog: PicSelectDialog
    private val findViewModel: FindViewModel by viewModels()
    private var adapter: BuynewcarHAdapterList? = null
    private var adapter1: BuynewcarVAdapterList? = null
    private var adapter2: BuynewcarLiShiAdapterList? = null
    override fun getViewBinding(
        inflater: LayoutInflater, container: ViewGroup?
    ): FragmentBuyNewCarBinding = FragmentBuyNewCarBinding.inflate(layoutInflater)



    override fun initData() {
        photoDialog = PicSelectDialog(context, requireActivity())
        photoDialog.setOnSelectListener(object :
            PicSelectDialog.onItemClickListener {
            @RequiresApi(Build.VERSION_CODES.O)
            override fun onFile(mPhoto: List<String?>) {
                PictureThreadUtils.runOnUiThread {
                    mPhoto[0]?.apply {
                        findViewModel.getLicenseByOCR(this,requireActivity().applicationContext)
                    }
                }
            }
        })
        binding.buyNewcarSearchBox.setOnClickListener {
            XXPermissions.with(this).permission(Permission.READ_MEDIA_IMAGES, Permission.CAMERA)
                .request { permissions, all ->
                    if (!all) {
                        ToastUtils.showLong("获取部分权限成功，但部分权限未正常授予")
                        return@request
                    }
                    photoDialog.show()
                }
        }

        //横向列表
        viewmodel.getbuy_newcarlist()
        viewmodel.buy_newcarListLiveData.observe(this, Observer {
            adapter = context?.let { it1 -> BuynewcarHAdapterList(it1, it.result) }
            binding.buyNewcarQueryrecy.layoutManager =
                LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
            binding.buyNewcarQueryrecy.adapter = adapter
        })
        //竖向列表
        viewmodel.getbuy_newcarlist()
        viewmodel.buy_newcarListLiveData.observe(this, Observer {
            adapter1 = context?.let { it1 -> BuynewcarVAdapterList(it1, it.result) }
            binding.buyNewcarVerlistRecy.layoutManager = LinearLayoutManager(context)
            binding.buyNewcarVerlistRecy.adapter = adapter1
        })
    }


}